<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@page import="java.util.ArrayList"%>
<%@page import="com.java.FamTree.*" %>
<%@ include file="loginDetails.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<BODY>

<%!
String personId = null;
String childId = null;

boolean addChild(JspWriter out,HttpServletRequest request,
					HttpSession session)
{
	personId = request.getParameter("PersonId");
	childId = request.getParameter("ChildId");
	
    
	if(personId == null || childId == null)
		return false;
	// Should make a database connection here and check password
	try { // Load driver class
		Class.forName("com.mysql.jdbc.Driver");
	}
	catch (java.lang.ClassNotFoundException e) {
		System.err.println("ClassNotFoundException: " +e);
	}
	
	Connection con2 = null;
		try { 
		    con2 = DriverManager.getConnection(url+db, uid, pwd); 
			PreparedStatement addChild = con2.prepareStatement("INSERT INTO Child (Parent, Child) VALUES (?,?)");
			addChild.setInt(1, Integer.parseInt(personId));
			addChild.setInt(2, Integer.parseInt(childId));
			
			addChild.executeUpdate();
			
		} catch (SQLException ex) { 
		    System.err.println(ex); 
		} finally {
		    if (con2 != null ) {
		        try {
		            con2.close();
		        } catch (SQLException ex) {
		            System.err.println(ex);
		        }
		    }
		}
	
	return true;
} 
%>


<%
	//Check to see if we're logged in.
	if (session.getAttribute("UserId") == null) {
	    response.sendRedirect("index.jsp"); // Send them away if they're not logged in.
	}


	boolean success = false;
	session = request.getSession(true);// May create new session
	//try{
		success = addChild(out,request,session);
	//}
	//catch(IOException e){ System.err.println(e); }
	
	if(!success)
		response.sendRedirect("ViewIndividual.jsp?PersonId="+personId); // Success
	else
		response.sendRedirect("ViewIndividual.jsp?PersonId="+personId); // Failed login
	// Redirect back to login page with a message
%>

</BODY>
</HTML>